<?php

namespace App\Http\Controllers\Log;

use App\Http\Controllers\Controller;
use App\Models\Archive\OperationLog;
use App\Models\Auth\User;
use App\Models\Common\ApiResponse;
use App\Models\Common\BaseModel;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;

class LogController extends Controller
{
    public function index(Request $request)
    {
        $user = admin();
        $parkingLotID = getParkingLotID(false, true);

        $page = intval($request->get('page') ?: 1);
        $perPage = intval($request->get('per_page') ?: 10);

        try {
            $dateTime = Carbon::parse($request->get('datetime'))->toDateTimeString();
        } catch (\Throwable $exception) {
            $dateTime = now()->toDateTimeString();
        }

        if (!$tableName = OperationLog::getTableName($dateTime)) {
            throw new \InvalidArgumentException('获取记录表名失败');
        }

        $baseQuery = (new OperationLog())->setTable($tableName)->orderByDesc('updated_at');

        if ($user->identity === User::IDENTITY_SYSTEM) {
            if ($parkingLotID) {
                $baseQuery->where('parking_lot_id', $parkingLotID);
            }
        } else {
            $baseQuery->whereIn('parking_lot_id', $user->parkingLotIds);
        }

        if ($loggableID = $request->get('loggable_id')) {
            $baseQuery->where('loggable_id', $loggableID);
        }

        if ($type = $request->get('type')) {
            $baseQuery->where('type', $type);
        }

        if ($keyword = $request->get('keyword')) {
            $baseQuery->where('keyword', 'like', "$keyword%");
        }

//        $paginationDat = $baseQuery->paginate(intval($request->get('per_page')), '*', 'page', intval($request->get('page')));

        $logs = BaseModel::getList($baseQuery, $page, $perPage, '', ['loggable_id','loggable_type','parking_lot_id','login_ip']);

        return ApiResponse::return('', $logs);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
